package handler

import (
	"github.com/name5566/leaf/gate"
	log2 "github.com/name5566/leaf/log"
	"server/game/card"
	"server/msg/jsonData"
)

func CreateCard(args []interface{}) {
	m := args[0].(*jsonData.MsgCreateCardReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	cardDataList, err := card.CreateCard(m.UserId, m.CTId)
	if err != nil {
		log2.Error("err is %s", err.Error())
		a.WriteMsg(&jsonData.MsgRes{
			Ret:       1,
			CardsList: nil,
			RequestId: m.RequestId,
		})
		return
	}
	cardList := make([]jsonData.CardInfo, 0)
	for _, CardInfo := range cardDataList {
		cardJsonInfo := jsonData.CardInfo{
			UserId:     CardInfo.UserId,
			CardId:     CardInfo.CardId,
			Star:       CardInfo.Star,
			Level:      CardInfo.Level,
			SkillList:  CardInfo.SkillList,
			WorkList:   CardInfo.WorkList,
			FateList:   CardInfo.FateList,
			SkinSkill:  CardInfo.SkinSkill,
			HolaList:   CardInfo.HolaList,
			UniqueList: CardInfo.UniqueList,
		}
		cardList = append(cardList, cardJsonInfo)
	}
	a.WriteMsg(&jsonData.MsgRes{
		Ret:       jsonData.OK,
		CardsList: cardList,
		RequestId: m.RequestId,
	})
}

func CardList(args []interface{}) {
	m := args[0].(*jsonData.MsgCardListReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	cardList, err := card.GetCardListByUserId(m.UserId)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: cardList, RequestId: m.RequestId})
}

func UpdateCardLevel(args []interface{}) {
	m := args[0].(*jsonData.MsgUpdateCardLevelReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	ret, err := card.UpdateLevel(m.UserId, m.CardId, m.Step)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: ret, RequestId: m.RequestId})
	return
}
func UpdateCardStar(args []interface{}) {
	m := args[0].(*jsonData.MsgUpdateCardStarReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	ret, err := card.UpdateStar(m.UserId, m.CardId, m.Step)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{
			Ret:       1,
			RequestId: m.RequestId,
		})
		return
	}
	cardDataList := ret["card_list"].([]card.CardInfo)
	cardList := make([]jsonData.CardInfo, 0)
	for _, CardInfo := range cardDataList {
		cardJsonInfo := jsonData.CardInfo{
			UserId:     CardInfo.UserId,
			CardId:     CardInfo.CardId,
			Star:       CardInfo.Star,
			Level:      CardInfo.Level,
			SkillList:  CardInfo.SkillList,
			WorkList:   CardInfo.WorkList,
			FateList:   CardInfo.FateList,
			SkinSkill:  CardInfo.SkinSkill,
			HolaList:   CardInfo.HolaList,
			UniqueList: CardInfo.UniqueList,
		}
		cardList = append(cardList, cardJsonInfo)
	}
	bagList := ret["bag_list"]
	a.WriteMsg(&jsonData.MsgRes{
		Ret:       jsonData.OK,
		CardsList: cardList,
		BagList:   bagList,
		RequestId: m.RequestId,
	})
}
func UpdateCardSkillLevel(args []interface{}) {
	m := args[0].(*jsonData.MsgUpdateCardSkillReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	level, err := card.UpdateSkillLevel(m.UserId, m.CardId, m.SkillType, m.SkillId, m.Step)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{
			Ret:       1,
			RequestId: m.RequestId,
		})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: level, RequestId: m.RequestId})
}
